import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam.dart';
import 'package:kxy_flutter_app/model/exam_category.dart';
import 'package:kxy_flutter_app/view/course/course_detail_view.dart';
import 'package:kxy_flutter_app/view/examination/cate_exam_list_view.dart';
import 'package:kxy_flutter_app/view/examination/const_cate_view.dart';
import 'package:kxy_flutter_app/view/examination/do_exam_view.dart';
import 'package:kxy_flutter_app/view/examination/test_list_view.dart';
import 'package:kxy_flutter_app/view/home/notice/notice_view.dart';
import 'package:kxy_flutter_app/view/my/collect/collect_view.dart';
import 'package:kxy_flutter_app/view/my/exam_history/exam_history_view.dart';
import 'package:kxy_flutter_app/view/my/integral/integral_view.dart';
import 'package:kxy_flutter_app/view/my/mistake/mistake_list_view.dart';
import 'package:kxy_flutter_app/view/my/rank/exam_rank_view.dart';
import 'package:kxy_flutter_app/view/news/news_detail_view.dart';

class NavRoute {
  static Future pushTo(String modulePath, BuildContext context, {String param}) async {
    switch (modulePath) {
      case '/pages/notice/notice':
        _goto(NoticeView(), context);
        break;
      case '/pages/exam/exam-list':
        var cate = await getExamCate(param);
        if (cate == null) {
          Fluttertoast.showToast(msg: '考试分类不存在');
        } else {
          _goto(CategoryExamListView(category: cate), context);
        }
        break;
      case '/pages/exam/test-list':
        var cate = await getExamCate(param);
        if (cate == null) {
          Fluttertoast.showToast(msg: '答题分类不存在');
        } else {
          _goto(TestListView(category: cate), context);
        }
        break;
      case '/pages/exam/exam-detail':
        var exam = await getExamById(param);
        if (exam == null) {
          Fluttertoast.showToast(msg: '考试考试不存在');
        } else {
          _goto(DoExamView(exam: exam), context);
        }
        break;
      case '/pages/exam/exam-history':
        _goto(ExamHistoryView(), context);
        break;
      case '/pages/ranking-list/ranking-list':
        _goto(ExamRankView(), context);
        break;
      case '/pages/collect/collect':
        _goto(CollectView(), context);
        break;
      case '/pages/news/news-detail':
        _goto(NewsDetailView(newsId: param), context);
        break;
      case '/pages/exam/practice':
        _goto(ConstCateView(), context);
        break;
      case '/pages/course/detail':
        _goto(CourseDetailView(courseId: param), context);
        break;
      case '/pages/integral/index':
        _goto(IntegralView(), context);
        break;
      case '/pages/mistakes/mistakes':
        _goto(MistakeListView(), context);
        break;
      default:
        break;
    }
  }

  static Future<Exam> getExamById(String examId) async {
    var res = await Http.instance.post('/exam-app/exam/info', params: {
      'examId': examId,
      'userId': Auth.instance.userId,
    });

    if (res.code == 200) {
      return Exam.fromJson(res.data);
    }
    return null;
  }

  static Future<ExamCategory> getExamCate(String cateId) async {
    List<ExamCategory> cateList = new List<ExamCategory>();
    var res = await Http.instance.post('/exam-app/exam/cate/list');
    if (res.code == 200) {
      cateList = (res.data as List).map((item) => ExamCategory.fromJson(item)).toList();
    }
    var index = cateList.indexWhere((x) => x.id == cateId);
    if (index > -1) {
      return cateList[index];
    }

    return null;
  }

  static Future _goto(Widget widget, BuildContext context) async {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (cxt) => widget,
      ),
    );
  }
}
